#include "plotmodel.h"
#include "cmath"
PlotModel::PlotModel(QObject* parent)
    : QAbstractTableModel(parent), _rootItems(new PlotTreeRootItem)
{
    connect(_rootItems.get(), &PlotTreeRootItem::chekStatusChanged, this, &PlotModel::chekStatusChanged);

    _header << "" << "name" << "value";

}
#include <qdebug.h>
PlotModel::~PlotModel()
{
  //  qDebug() << "~PlotModel()";
}
/// <summary>
/// 显示曲线数据
/// </summary>
/// <param name="data">数据</param>
/// <param name="plot_name">label</param>
void PlotModel::add_plotData(QSharedPointer<QCPGraphDataContainer>& data, QString plot_name ) {
    for (auto const& var : _rootItems->data()) {
        if (var->_data.get() == data.get()) {}//已经存在了
    }
    _rootItems->addData(data, plot_name);
}


int PlotModel::rowCount(const QModelIndex& parent) const
{
    return _rootItems->data().count();
}

int PlotModel::columnCount(const QModelIndex& parent) const
{
    return _header.count();
}

QVariant PlotModel::data(const QModelIndex& index, int role) const
{
    double dtemp;
    if (role == Qt::DisplayRole && index.column() != 0)
    {
        switch (index.column())
        {
        default:
            break;
        //case 0://复选框和颜色
        //    return  _rootItems->data().at(index.row())->getcolor();
        case 1:
            return _rootItems->data().at(index.row())->getname();
        case 2:
            dtemp = _rootItems->data().at(index.row())->getValue();
            if (dtemp == -1e31) return QVariant();
            else return dtemp;
        case 3:
			return _rootItems->data().at(index.row())->getunit();
        }

    }
    else if (role == Qt::CheckStateRole && index.column() == 0)//第一行为复选框
    {
        return   _rootItems->data().at(index.row())->getcheckStatus() ? Qt::Checked : Qt::Unchecked;
    }
    else if (role == Qt::DecorationRole && index.column() == 0)
    {
        //TODO:X_这里一定要优化
        QPixmap pixmap(32, 32);
        // 使用QPainter在QPixmap上绘制
        QPainter painter(&pixmap);
        // 绘制一个纯色背景
        painter.fillRect(pixmap.rect(), _rootItems->data().at(index.row())->getcolor()); // 使用红色填充
        painter.setPen(QPen(Qt::black, 2)); // 设置画笔为黑色，宽度为2
        painter.drawRect(pixmap.rect().adjusted(1, 1, -1, -1)); // 绘制边框，稍微小于图标边界

        painter.end(); // 结束绘制
        return QIcon(pixmap);
    }
    else if (role == Qt::DecorationPropertyRole)
    {
        return Qt::AlignCenter;
    }
    else if (role == Qt::TextAlignmentRole && index.column() == 4)
    {
        return Qt::AlignCenter;
    }
    return QVariant();
}

QVariant PlotModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    {
        return _header[section];
    }
    return QAbstractTableModel::headerData(section, orientation, role);
}

bool PlotModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
    if (role == Qt::CheckStateRole && index.column() == 0) {
        // 更新项的选中状态
        if (_rootItems->data().at(index.row())->getcheckStatus() != value.toBool()) {
			_rootItems->data()[index.row()]->setcheckStatus(value.toBool());
       }
        return true;
    }
    return QAbstractTableModel::setData(index, value, role);
}

Qt::ItemFlags PlotModel::flags(const QModelIndex& index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;
    Qt::ItemFlags flags = QAbstractTableModel::flags(index);
    if (index.column() == 0)
        flags |= Qt::ItemIsUserCheckable;
    return flags;
}

void PlotTreeRootItem::addData( PlotData& plot)
{
   this->_data.append(QSharedPointer<PlotData>(new PlotData(plot)));
   //REVIEW: View 曲线选择状态改变传递给外部
   connect(this->_data.last().get(), &PlotData::checkStatusChanged, this, &PlotTreeRootItem::show_changed);
}

void PlotTreeRootItem::addData(QSharedPointer<QCPGraphDataContainer>& data, QString plot_name)
{
  //  qDebug() << this->_data.count() % (sizeof(PLOT::color) / sizeof(QColor)) << QString(PLOT::color[this->_data.count() % (sizeof(PLOT::color) / sizeof(QColor))]);
    this->_data.append(
        QSharedPointer<PlotData>( new PlotData(data, QColor(QString(PLOT::color[this->_data.count() % (sizeof(PLOT::color)/sizeof(QColor))])), plot_name, "unit", this)));
    //REVIEW: View 曲线选择状态改变传递给外部
    connect(this->_data.last().get(), &PlotData::checkStatusChanged, this, &PlotTreeRootItem::show_changed,Qt::UniqueConnection);
}

bool PlotTreeRootItem::removeData(const PlotData& plot)
{
    //for (auto  & const var : this->_data) {
    //    if (*var.get() == plot) {
    //        this->_data.removeAt(this->_data.indexOf(var));
    //        return true;
    //    }
    //}
    return false;
}
/// <summary>
/// 设置当前的曲线数据
/// </summary>
/// <param name="var"></param>
void PlotTreeRootItem::setCurrPlotData( const QSharedPointer<PlotData>& var) 
{
    int i = 0;
    for (auto& v : _data) {
        if (v.get() == var.get()) {
           _curr_index = i;
            emit chekStatusChanged(v);
            return;
        }
        i++;
    }
    _curr_index = -1;
}
void PlotTreeRootItem::setCurrPlotData(int index)
{
    if (index < _data.count()) {
        setCurrPlotData(_data.at(index));
    }
}
void PlotTreeRootItem::show_changed() {
    for (auto& var : _data) {
        if (var.get() == sender()) {
            setCurrPlotData(var);
            return;
        }
    }

}